/* GMFavMaster.js <?php
#   --------------------------------------------------------------
#   GMFavMaster.js 2011-09-05 gambio
#   Gambio GmbH
#   http://www.gambio.de
#   Copyright (c) 2011 Gambio GmbH
#   Released under the GNU General Public License (Version 2)
#   [http://www.gnu.org/licenses/gpl-2.0.html]
#   --------------------------------------------------------------
?>*/

function GMFavMaster()
{
	var current_box_id = '';
	
	$('#gm_box_favorites').ready(function()
	{
		if(fb)console.log('gm_box_favorites ready');
    
    
    $(".fav_drag_item").live("mouseover", function() {
      if (!$(this).data("init")) {
        $(this).data("init", true);
        $(this).draggable({
          helper: 'clone',
          start: function(ev, ui)
          {

            $("body").prepend('<div id="gm_fav_dropzone" class="gm_fav_add"><img src="images/gm_icons/fav_add.gif" /></div>');
            $("#gm_fav_dropzone").css('opacity', '0.9');

            var box_x = ev.pageX + 80;
            var box_y = ev.pageY - 75;

            if(fb)console.log('box_x:' + box_x +' '+ ev.pageX);
            if(fb)console.log('box_y:' + box_y +' '+ ev.pageY);

            $("#gm_fav_dropzone").css({
                                    'left'		: box_x + 'px',
                                    'top'			: box_y + 'px'
                                  });

            $("#gm_fav_dropzone").droppable({
              accept: ".fav_drag_item",
              tolerance: 'pointer',
              over: 			function(ev, ui) {$("#gm_fav_dropzone").css('opacity', 		'1.0');	},
              out: 				function(ev, ui) {$("#gm_fav_dropzone").css('opacity', 		'0.9');	},
              drop: function(ev, ui)
              {
                var link_key = $(ui.draggable).attr('id');
                gmFavMaster.save_fav(link_key);
                if(fb)console.log('received:' + link_key);
              }
            });
          },
          stop: function(ev, ui)
          {
            $("#gm_fav_dropzone").remove();
          }
        });
      }
    });
    
    $(".fav_content_item").live("mouseover", function() {
      if (!$(this).data("init")) {
        $(this).data("init", true);
        $(this).draggable({    
          helper: 'clone',
          start: function(ev, ui)
          {
            $("#BOX_HEADING_FAVORITES").prepend('<div id="gm_fav_dropzone" class="gm_fav_remove"><img src="images/gm_icons/fav_remove.gif" /></div>');
            $("#gm_fav_dropzone").css('opacity', '0.9');

            var box_x = ev.pageX + 80;
            var box_y = ev.pageY - 20;

            if(fb)console.log('box_x:' + box_x);
            if(fb)console.log('box_y:' + box_y);

            $("#gm_fav_dropzone").css({
                                    'left'		: box_x + 'px',
                                    'top'			: box_y + 'px'
                                  });

            $("#gm_fav_dropzone").droppable({
              accept: ".fav_content_item",
              tolerance: 'pointer',
              over: 			function(ev, ui) {$("#gm_fav_dropzone").css('opacity', 		'1.0');	},
              out: 				function(ev, ui) {$("#gm_fav_dropzone").css('opacity', 		'0.9');	},
              drop: function(ev, ui)
              {
                var link_key = $(ui.draggable).attr('id');
                gmFavMaster.delete_fav(link_key);
                if(fb)console.log('received:' + link_key);
              }
            });
          },
          stop: function(ev, ui)
          {
            $("#gm_fav_dropzone").remove();
          }
        });	
      }
    });
    	
	});
	
	
	/*
   ***********    OLD FUNCTION     ***********
	this.load_favs = function()
	{
		$('#gm_box_favorites').html('');
		$.ajax({
		  url: 			'../gm_ajax.php?module=admin_fav_master&act=load_favs',
		  dataType: 'json',
		  cache: 		false,
		  success: 	function(cresult)
		  					{
		  						if(fb)console.log('keys:' + cresult.link_keys.length);
		  						var curkey		= '';
		  						var link_text = '';
		  						var link_href = '';
		    					for(var i=0; i<cresult.link_keys.length; i++) {
		    						curkey = cresult.link_keys[i];
		    						if(fb)console.log('key:' + curkey);
		    						
		    						link_text = $('#' + curkey).text();
		    						link_href = $('#' + curkey).attr('href');
		    						
		    						$('#gm_box_favorites').append('<div class="leftmenu_body_item"><span class="'+ curkey +'"><a class="fav_content_item" href="'+ link_href +'">'+ link_text +'</a></span></div>');
		    					}
		  					}
		});
	}
	*/
	
	this.save_fav = function(link_key) {
		$.ajax({
		  url: 			'../gm_ajax.php?module=admin_fav_master&act=save_fav&link_key='+link_key,
		  success: 	function(res){
        list_link = $("<a></a>").addClass("fav_content_item ui-draggable").attr("id", $("#"+link_key).attr("id")).attr("href", $("#"+link_key).attr("href")).html($("#"+link_key).html());
        list_element = $("<li></li>").addClass("leftmenu_body_item").append(list_link);
        if($("#BOX_HEADING_FAVORITES").find("#"+link_key).length == 0){
          $("#BOX_HEADING_FAVORITES").append(list_element);
        }
      }
		});
	}
	
	this.delete_fav = function(link_key) {
		$.ajax({
		  url: 			'../gm_ajax.php?module=admin_fav_master&act=delete_fav&link_key='+link_key,
		  success: 	function(res){ 
        $("#"+link_key).parent().remove();
      }
		});
	}
	
}